<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-04-27
 * Time: 下午 7:11
 */
namespace App;
use Model\ActivityAdminMod;
use Model\ActivityMod;
use Pub\date\Date;

class Admin extends Action
{
	private $Admin = null;
	private $Activity = null;
	public function __construct()
	{
		parent::__construct();
		$this->Admin = new ActivityAdminMod();
	}

	/**
	 * 是否登录
	 */
	public function testLogin()
	{
		if(isset($_SESSION['vbz_activity_'.$this->_gpc['activity_id']])){
			$this->_result = array('status'=>'success','data'=>$_SESSION['vbz_activity_'.$this->_gpc['activity_id']]);
		}else{
			if(isset($this->_gpc['vbz_activity_'.$this->_gpc['activity_id']])){
				$this->Activity = new ActivityMod();
				$_activity = $this->Activity->findOne(array('id'=>$this->_gpc['activity_id']),array('name'));
				$this->_result = array('status'=>'success','data'=>$_activity);
			}
		}
		$this->result($this->_result);
	}

	/**
	 * 登录
	 */
	public function login()
	{
		$_salt = $this->Admin->findOne(array('activity_id'=>$this->_gpc['activity_id']),array('salt'));
		if(!$_salt){
			$this->_result = array('status'=>'error','err_status'=>9,'err_msg'=>'活动未开启管理后台');
			$this->result($this->_result);
		}
		$_salt = $_salt['salt'];
		$_account = $this->Admin->findOne(array('activity_id'=>$this->_gpc['activity_id'],'account'=>$this->_gpc['account'],'password'=>sha1($this->_gpc['password'].$_salt)));
		if($_account){
			$this->Activity = new ActivityMod();
			$_activity = $this->Activity->findOne(array('id'=>$this->_gpc['activity_id']),array('name'));
			$_SESSION['vbz_activity_'.$this->_gpc['activity_id']] = array('name'=>$_activity['name']);
			$this->_result = array('status'=>'success','data'=>$_SESSION['vbz_activity_'.$this->_gpc['activity_id']]);
		}else{
			$this->_result = array('status'=>'error','err_status'=>8,'err_msg'=>'登录失败，账号或者密码错误！');
		}
		$this->result($this->_result);
	}

	/**
	 * 后台管理自动登录
	 */
	public function autoLogin()
	{
		$this->Activity = new ActivityMod();
		$_activity = $this->Activity->findOne(array('id'=>$this->_gpc['activity_id']),array('name'));
		$_SESSION['vbz_activity_'.$this->_gpc['activity_id']] = array('name'=>$_activity['name']);
		if(isetcookie('vbz_activity_'.$this->_gpc['activity_id'],$_activity['name'] ? $_activity['name']:'名称未设置',TIMESTAMP+3600*2)){
			$this->_result = array('status'=>'success','data'=>murl('entry//admin',array('m'=>'kuyuan_vbz'),true,true).'&activity_id='.$this->_gpc['activity_id']);
		}
		$this->result($this->_result);
	}

	/**
	 * 退出登录
	 */
	public function logout()
	{
		unset($_SESSION['vbz_activity_'.$this->_gpc['activity_id']]);
		isetcookie('vbz_activity_'.$this->_gpc['activity_id'],'',TIMESTAMP-100);
		$this->_result = array('status'=>'success');
		$this->result($this->_result);
	}

	/**
	 * 验证活动是否存在
	 */
	public function checkActivity()
	{
		$this->Activity = new ActivityMod();
		$_activity = $this->Activity->findOne(array('id'=>$this->_gpc['activity_id'],'status !='=>9),array('id'));
		if($_activity) $this->_result = array('status'=>'success');
		$this->result($this->_result);
	}
}